<?php
namespace app\api\controller\newfile;

use think\Request;
use app\common\model\newmodel\Device as DeviceModel;
use app\common\model\newmodel\Accessories as AccessoriesModel;
use app\common\model\power\Rolepower;
use app\common\model\newmodel\User as UserModel;
use app\api\controller\Pub;
use app\api\controller\Base;

class Device extends Base
{
    public function initialize(){
		parent::initialize();
		self::is_login();
		Rolepower::powerguard(201201);
	}

    /**
	 * 判断账号是否登录
	 * @return boolean [description]
	 */
	private function is_login(){
		$data = UserModel::is_sessionlogin();
		if (!$data['code']) {
			$data['options'] = 'relogin';
			json($data)->send();
			exit;
		}
	}

    /**
     * 没有分页的合同列表
     */
    public function deviceListMore()
    {
        $result=DeviceModel::deviceListMore();
        return json($result);
    }

    /**
     * 设备列表
     */
    public function deviceList()
    {
        $data=input();
        $page=$data['page'] ?? 1;
        $pageSize=$data['pageSize'] ?? 10;
        $keyword=$data['keyword'] ?? '';
        $factory_id=input('factory_id');
        //设备列表包含配件信息
        $data=DeviceModel::DeviceList($page,$pageSize,$keyword,$factory_id);
        return myok($data);
    }
    /**
     * 添加设备
     */

     public function deviceAdd()
    {
        $data = input('post.');
        $result = DeviceModel::addDevice($data);
        return json($result);
    }

    /**
     * 修改设备
     */
    public function deviceEdit()
    {
        $data = input('post.');
        $result = DeviceModel::updateDevice($data);
        return json($result);
    }

    /**
     * 删除设备
     */
    public function deviceDel()
    {
        $device_id = input('id');
        $result = DeviceModel::deleteDevice($device_id);
        return json($result);
    }

    /**
     * 没有分页的配件列表
     */
    public function accessoryListTwo()
    {
        $result=AccessoriesModel::accessoryList();
        return json($result);
    }
    
    /**
     * 配件列表
     */
    public function accessoryList()
    {
        $data=input();
        $page=$data['page'] ?? 1;
        $pageSize=$data['pageSize'] ?? 10;
        $keyword=$data['keyword'] ?? '';
        $result=AccessoriesModel::getAccessoryList($page,$pageSize,$keyword);
        return json($result);
    }

    /**
     * 添加配件
     */
    public function accessoryAdd()
    {
        $data=input();
        $result = AccessoriesModel::addAccessory($data);
        return json($result);
    }

    /**
     * 修改配件
     */
    public function accessoryEdit()
    {
        $data = input('post.');
        $result = AccessoriesModel::updateAccessory($data);
        return json($result);
    }

    /**
     * 删除配件
     */
    public function accessoryDel()
    {
        $accessory_id = input('id');
        $result = AccessoriesModel::deleteAccessory($accessory_id);
        return json($result);
    }
}